package com.kancy.jemeter.util;


import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 获取随机数
 * @author kancy
 * @version 1.0
 * @date 2019/5/27 13:31
 */
public class RandomUtil {

    public static int nextInt(){
        return ThreadLocalRandom.current().nextInt();
    }
    public static int nextInt(int bound){
        return ThreadLocalRandom.current().nextInt(bound);
    }
    public static int nextInt(int start,int end){
        return ThreadLocalRandom.current().nextInt(start, end);
    }
    public static int[] ints(int size, int start, int end){
        int[] ints = new int[size];
        for (int i = 0; i < size; i++) {
            ints[i] = nextInt(start, end);
        }
        return ints;
    }

    public static long nextLong(){
        return ThreadLocalRandom.current().nextLong();
    }
    public static long nextLong(long bound){
        return ThreadLocalRandom.current().nextLong(bound);
    }
    public static long nextLong(long start,long end){
        return ThreadLocalRandom.current().nextLong(start, end);
    }
    public static long[] longs(int size, long start, long end){
        long[] longs = new long[size];
        for (int i = 0; i < size; i++) {
            longs[i] = nextLong(start, end);
        }
        return longs;
    }

    public static double nextDouble(){
        return ThreadLocalRandom.current().nextDouble();
    }
    public static double nextDouble(double bound){
        return ThreadLocalRandom.current().nextDouble(bound);
    }
    public static double nextDouble(double start,double end){
        return ThreadLocalRandom.current().nextDouble(start, end);
    }
    public static double[] doubles(int size, double start, double end){
        double[] doubles = new double[size];
        for (int i = 0; i < size; i++) {
            doubles[i] = nextDouble(start, end);
        }
        return doubles;
    }
    public static double[] doubles(int size, double start, double end, int scale){
        double[] doubles = new double[size];
        for (int i = 0; i < size; i++) {
            doubles[i] = nextDouble(start, end);
        }
        return doubles;
    }

    public static double nextFloat(){
        return ThreadLocalRandom.current().nextDouble();
    }
    public static boolean nextBoolean(){
        return ThreadLocalRandom.current().nextBoolean();
    }
    /**
     * 得到一组符合高斯/正态分布的随机数，是否可以利用nextGaussian方法
     * @return
     */
    public static double nextGaussian(){
        return ThreadLocalRandom.current().nextGaussian();
    }

    public static int[] createIntArray(int start,int end, int len){
        int arr[] = new int[len];
        for (int i = 0; i <len ; i++) {
            arr[i] = ThreadLocalRandom.current().nextInt(start, end);
        }
        return arr;
    }

    public static int[] createIntArrayNoRepeat(int start,int end, int len){
        if(end - start < len){
            throw new IllegalArgumentException("need: end - start >= len");
        }
        if(end - start == len){
            int arr[] = new int[len];
            int tmp = start;
            for (int i = 0; i <len ; i++) {
                arr[i] = tmp++;
            }
            return arr;
        }
        Set<Integer> sets = new HashSet(len);
        while(sets.size() < len){
            sets.add(ThreadLocalRandom.current().nextInt(start, end));
        }
        int arr[] = new int[len];
        for (Integer integer : sets) {
            arr[len--] = integer;
        }
        return arr;
    }

}
